package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

/**
 * This is the starting point for the app.
 */
public class StartActivity extends Activity {

    private int alpha = 0;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            final Object alphaFromState = savedInstanceState.get("alpha");
            alpha = alphaFromState != null ? (Integer) alphaFromState : 0;
        }

        // From the main.xml template
        setContentView(R.layout.main);

        configureCanvast();
        createDarkerButton();
        createLighterButton();
    }

    private void configureCanvast() {
        final View imageView = findViewById(R.id.imageView);
        imageView.setOnTouchListener(new TwoFingerGestureListener());
        final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        imageView.setMinimumHeight(displayMetrics.heightPixels);
        imageView.setMinimumWidth(displayMetrics.widthPixels);
    }

    private void createLighterButton() {
        final Button buttonDarker = (Button) findViewById(R.id.buttonDarker);
        buttonDarker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                final ImageView imageView = (ImageView) findViewById(R.id.imageView);
                alpha = alpha > 0 ? alpha - 10 : 0;
                imageView.setAlpha(alpha);
                log("" + StartActivity.this.alpha);
                imageView.invalidate();
            }
        });
    }

    private void createDarkerButton() {
        final Button buttonLighter = (Button) findViewById(R.id.buttonLighter);
        buttonLighter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                final ImageView imageView = (ImageView) findViewById(R.id.imageView);
                alpha = alpha < 255 ? alpha + 10 : 255;
                imageView.setAlpha(alpha);
                log("" + StartActivity.this.alpha);
                imageView.invalidate();
            }
        });
    }

    @Override
    protected void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("alpha", alpha);
        outState.putInt("x", alpha);
        outState.putInt("y", alpha);
    }

    private void log(String msg) {
        Log.d("JK: changed alpha ", msg);
    }
}
